package com.book.store.present.page;

import com.book.store.domain.store.entity.Book;
import com.book.store.present.service.BookStoreService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

@Controller
public class HomePage {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private BookStoreService bookService;

    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping("")
    public String home(Model model) throws JsonProcessingException {
        Collection<Book> books = bookService.getBooks();
        model.addAttribute("bookList", objectMapper.writer().writeValueAsString(books));
        return "home";
    }
}
